package com.lims.landInfo.action;

import java.io.File;
import java.io.IOException;

import org.apache.struts2.ServletActionContext;
import org.springframework.util.FileCopyUtils;

import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.ModelDriven;
import javacommon.base.*;
import javacommon.util.TextTool;

import com.lims.landInfo.model.*;
import com.lims.landInfo.service.*;
import com.lims.landInfo.vo.query.*;

/**
 * @author zzh
 * @version 1.0
 * @since 1.0
 */
@SuppressWarnings({ "serial", "unchecked" })
public class LandFileAction extends BaseStruts2Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	//forward paths
	protected static final String QUERY_JSP = "/pages/landInfo/LandFile/query.jsp";
	protected static final String LIST_JSP= "/pages/landInfo/LandFile/list.jsp";
	protected static final String CREATE_JSP = "/pages/landInfo/LandFile/create.jsp";
	protected static final String EDIT_JSP = "/pages/landInfo/LandFile/edit.jsp";
	protected static final String SHOW_JSP = "/pages/landInfo/LandFile/show.jsp";
	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/pages/landInfo/LandFile/list.do";
	
	private LandFileManager landFileManager;
	
	private LandFile landFile;
	java.lang.String id = null;
	private String items;

	private File uploadFile;//得到上传的文件
	private String uploadFileContentType;//得到文件的类型
	private String uploadFileFileName;//得到文件的名称
	private File sourceFile;
	private String sourceFileContentType;//得到文件的类型
	private String sourceFileFileName;//得到文件的名称

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			landFile = new LandFile();
		} else {
			landFile = (LandFile)landFileManager.getById(id);
		}
	}
	
	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setLandFileManager(LandFileManager manager) {
		this.landFileManager = manager;
	}	
	
	public Object getModel() {
		return landFile;
	}
	
	public void setObjid(java.lang.String val) {
		this.id = val;
	}

	public void setItems(String items) {
		this.items = items;
	}
	
	/** 进入list页面 */
	public String list() {
		return LIST_JSP;
	}
	
	public String goAdd(){
		return CREATE_JSP;
	}
	

	/**执行搜索**/
	public String findList() {
		LandFileQuery query = newQuery(LandFileQuery.class,DEFAULT_SORT_COLUMNS);
		ListRange listRange = this.landFileManager.findRange(query);
		this.responseOutWithJson(listRange);
		return null;
	}
	
	/** 保存新增对象 
	 * @throws IOException */
	public String save() throws IOException {
		String realpath = ServletActionContext.getServletContext().getRealPath("/upload/imgs");
		if(this.uploadFile != null) {
			String fn0 = this.getUploadFileFileName();
			String suffix = fn0.substring(fn0.lastIndexOf("."),fn0.length());
			String tempName = this.uploadFile.getName();
			String newName = tempName.substring(0,tempName.lastIndexOf("."));
			File file = new File(new File(realpath),newName+suffix);
			FileCopyUtils.copy(this.uploadFile, file);
			landFile.setFilePath("upload/imgs/"+newName+suffix);
		}
		if(this.sourceFile != null) {
			String fn0 = this.getSourceFileFileName();
			String suffix = fn0.substring(fn0.lastIndexOf("."),fn0.length());
			String tempName = this.sourceFile.getName();
			String newName = tempName.substring(0,tempName.lastIndexOf("."));
			File file = new File(new File(realpath),newName+suffix);
			FileCopyUtils.copy(this.uploadFile, file);
			landFile.setDownFilePath("upload/imgs/"+newName+suffix);
		}
		Land land = new Land();
		land.setObjid(landFile.getLandId());
		landFile.setLand(land);
		landFileManager.save(landFile);
		this.requestMap.put("flag", "true");
		return CREATE_JSP;
	}
	
	/**保存更新对象*/
	public String update() {
		landFileManager.update(this.landFile);
		this.responseOutWithJson("{\"success\":true,\"msg\":\"更新成功\"}");
		return null;
	}
	
	/**删除对象*/
	public String delete() {
		String realpath = ServletActionContext.getServletContext().getRealPath("/");
		String[] objids = items.split(",");
		for (int i = 0; i < objids.length; i++) {	
			//删除对象时删除文件
			LandFile lf = landFileManager.getById(objids[i]);
			if(TextTool.isNotNull(lf.getFilePath())){
				File file = new File(realpath+"/"+lf.getFilePath());
				if(file.exists()) file.delete();
			}
			if(TextTool.isNotNull(lf.getDownFilePath())) {
				File file = new File(realpath+"/"+lf.getDownFilePath());
				if(file.exists()) file.delete();
			}
			landFileManager.removeById(objids[i]);
		}
		this.responseOutWithJson("{\"success\":true,\"msg\":\"删除成功\"}");
		return null;
	}
	

	public File getUploadFile() {
		return uploadFile;
	}

	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}

	public String getUploadFileContentType() {
		return uploadFileContentType;
	}

	public void setUploadFileContentType(String uploadFileContentType) {
		this.uploadFileContentType = uploadFileContentType;
	}

	public String getUploadFileFileName() {
		return uploadFileFileName;
	}

	public void setUploadFileFileName(String uploadFileFileName) {
		this.uploadFileFileName = uploadFileFileName;
	}

	public File getSourceFile() {
		return sourceFile;
	}

	public void setSourceFile(File sourceFile) {
		this.sourceFile = sourceFile;
	}

	public String getSourceFileContentType() {
		return sourceFileContentType;
	}

	public void setSourceFileContentType(String sourceFileContentType) {
		this.sourceFileContentType = sourceFileContentType;
	}

	public String getSourceFileFileName() {
		return sourceFileFileName;
	}

	public void setSourceFileFileName(String sourceFileFileName) {
		this.sourceFileFileName = sourceFileFileName;
	}
	
}
